package cn.com.taiji.oospub.wechat.common.wxpay.util;

import cn.com.taiji.oospub.wechat.common.wxpay.entity.ScanPayReqData;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;


public class ObjectToXML {

	public String resolveRegistInfo(String requestRegistInfoXml){
		String errorCode = "9999";
		try {
			Document doc = DocumentHelper.parseText(requestRegistInfoXml);
			Element rootment = doc.getRootElement();// 获取根节点
			if (null == rootment || !"ARAMARK_RESPONSE".equals(rootment.getName())) {
				return  errorCode;
			}else{
				Element head = rootment.element("HEAD");
				if(head == null ) {
                    return errorCode;
                }
				
				Element RESPONSE_ELEMENT = head.element("RESPONSE_CODE");
				if(RESPONSE_ELEMENT == null) {
                    return errorCode;
                }
				
				if("0000".equals(RESPONSE_ELEMENT.getTextTrim())){
					Element BODY  = rootment.element("BODY");
					if(BODY == null) {
                        return errorCode;
                    }
					
					Element RESULT = BODY.element("RESULT");
					if(RESULT == null || "9999".equals(RESULT)) {
                        return errorCode;
                    }
					
					return RESULT.getTextTrim();
					
				}else{
					return errorCode;
				}
			}
		} catch (DocumentException e) {
			e.printStackTrace();
			return errorCode;
		}
	}
	
		/**
		 * 解析 weix支付返回的 xml文件
		 * @param requestRegistInfoXml
		 * @return
		 */
		public String resolveOrderInfo(String requestRegistInfoXml){
			String errorCode = "9999";
			try {
				Document doc = DocumentHelper.parseText(requestRegistInfoXml);
				Element rootment = doc.getRootElement();// 获取根节点
				if (null == rootment || !"ARAMARK_RESPONSE".equals(rootment.getName())) {
					return  errorCode;
				}else{
					Element head = rootment.element("HEAD");
					if(head == null ) {
                        return errorCode;
                    }
					
					Element RESPONSE_ELEMENT = head.element("RESPONSE_CODE");
					if(RESPONSE_ELEMENT == null) {
                        return errorCode;
                    }
					
					if("0000".equals(RESPONSE_ELEMENT.getTextTrim())){
						Element BODY  = rootment.element("BODY");
						if(BODY == null) {
                            return errorCode;
                        }
						
						Element RESULT = BODY.element("RESULT");
						if(RESULT == null || "9999".equals(RESULT)) {
                            return errorCode;
                        }
						
						return RESULT.getTextTrim();
						
					}else{
						return errorCode;
					}
				}
			} catch (DocumentException e) {
				e.printStackTrace();
				return errorCode;
			}
		}
		/**
		 * 生成 微信支付 请求xml
		 * @return
		 */
		public String requestWXPAYInfo(ScanPayReqData payData){
			
//			String requestRegistXml="<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
			String requestRegistXml="<xml encoding=\"UTF-8\">";
			requestRegistXml+="<appid>"+payData.getAppid()+"</appid>";
			requestRegistXml+="<mch_id>"+payData.getMch_id()+"</mch_id>";
			requestRegistXml+="<nonce_str>"+payData.getNonce_str()+"</nonce_str>";
			requestRegistXml+="<sign>"+payData.getSign()+"</sign>";
			requestRegistXml+="<body>"+payData.getBody()+"</body>";
			requestRegistXml+="<attach>"+payData.getAttach()+"</attach>";
			requestRegistXml+="<out_trade_no>"+payData.getOut_trade_no()+"</out_trade_no>";
			requestRegistXml+="<total_fee>"+payData.getTotal_fee()+"</total_fee>";
			requestRegistXml+="<spbill_create_ip>"+payData.getSpbill_create_ip()+"</spbill_create_ip> ";
			requestRegistXml+="<notify_url>"+payData.getNotify_url()+"</notify_url> ";
			//JSAPI--公众号支付、NATIVE--原生扫码支付、APP--app支付，统一下单接口trade_type的传参可参考这里
			//MICROPAY--刷卡支付，刷卡支付有单独的支付接口，不调用统一下单接口
			requestRegistXml+="<trade_type>"+payData.getTrade_type()+"</trade_type> ";
			requestRegistXml+="<product_id>"+payData.getOut_trade_no()+"</product_id> ";//商品编码
			requestRegistXml+="</xml>";
			return requestRegistXml;
		}
}
